package com.education.basic.service.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.education.basic.dao.FeeInfoDAO;
import com.education.basic.dao.StudentDao;
import com.education.basic.entity.FeeInfo;
import com.education.basic.entity.Student;
import com.education.basic.service.FeeInfoService;
import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.Logger;
import com.yunshangwoniu.core.utils.RequestUtils;

@Service("feeInfoService")
public class FeeInfoServiceImpl extends BaseServiceImpl<FeeInfo> implements FeeInfoService 
{
	@Resource
	private StudentDao studentDao;
	@Resource
	private FeeInfoDAO feeInfoDAO;
	@Resource
	private PageService<FeeInfo> pageService;
	
	@Override
	@Transactional
	public boolean pay(String stuNo)
	{
		Student stu = studentDao.getByStuNO(stuNo);
		if(stu==null)
		{
			Logger.debug(this.getClass(),String.format("pay fail beacause student not exist when [stuNum:]",stuNo));
			return false;
		}
		FeeInfo info = feeInfoDAO.getByStuNum(stuNo);
		if(info==null)
		{
			Logger.debug(this.getClass(),String.format("pay fail beacause feeInfo not exist when [stuNum:]",stuNo));
			return false;
		}
		info.setIsFee(true);
		feeInfoDAO.update(info);
		Logger.debug(this.getClass(),String.format("pay success when [stuNum:]",stuNo));
		return true;
	}

	@Override
	@Transactional
	public FeeInfo findFeeInfoByStudentName(String name) 
	{
		return feeInfoDAO.getByStudentName(name);
	}

	@Override
	@Transactional
	public FeeInfo findFeeInfoByStuNum(String stuNum)
	{
		return feeInfoDAO.getByStuNum(stuNum);
	}

	@Override
	@Transactional
	public Page<FeeInfo> queryFeeInfo(Map<String, Object> params,int currentPageNum, int pageSize, HttpServletRequest request) 
	{
		Page<FeeInfo> page = new Page<FeeInfo>(currentPageNum, pageSize,RequestUtils.getPageUrl(request));
		page.setParams(params);
		page = feeInfoDAO.query(page, FeeInfo.class);
		pageService.htmlBuilder(page, "default");
		try
		{
			Logger.debug(this.getClass(), String.format("get FeeInfo success  size is %s ", page.getRecords().size()));
		}catch(Exception e)
		{
			Logger.debug(this.getClass(), String.format("get FeeInfo failed"));
		}
		return page;
	}

}
